package com.lm.nacosconfig.component;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

@Component
public class DynamicThreadPoolManager {

    private final ThreadPoolTaskExecutor executor;

    public DynamicThreadPoolManager() {
        this.executor = new ThreadPoolTaskExecutor();
        // 初始化线程池默认配置
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(100);
        executor.setKeepAliveSeconds(60);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
    }

    public ThreadPoolTaskExecutor getExecutor() {
        return executor;
    }

    public void updateThreadPool(int corePoolSize, int maxPoolSize, int queueCapacity) {
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        System.out.println("线程池参数已更新：corePoolSize=" + corePoolSize + ", maxPoolSize=" + maxPoolSize + ", queueCapacity=" + queueCapacity);
    }
}
